import apiClient from '@/api/axios.js';

export class ChatMessageApi {

  // 查询所有聊天消息
  static async getAllChatMessages() {
    try {
      const response = await apiClient.get('/chatmessage');
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 messageID 查询聊天消息
  static async getChatMessageById(messageID) {
    try {
      const response = await apiClient.get(`/chatmessage/${messageID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 chatSessionID 查询聊天消息
  static async getChatMessagesBySessionId(chatSessionID) {
    try {
      const response = await apiClient.get(`/chatmessage/session/${chatSessionID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 senderID 查询聊天消息
  static async getChatMessagesBySenderId(senderID) {
    try {
      const response = await apiClient.get(`/chatmessage/sender/${senderID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 新增聊天消息
  static async createChatMessage(params) {
    const { chatSessionID, senderID, content, sendTime, status } = params;
    try {
      const response = await apiClient.post('/chatmessage', {
        chatSessionID,
        senderID,
        content,
        sendTime,
        status
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 更新聊天消息
  static async updateChatMessage(messageID, params) {
    const { chatSessionID, senderID, content, sendTime, status } = params;
    try {
      const response = await apiClient.put(`/chatmessage/${messageID}`, {
        messageID,
        chatSessionID,
        senderID,
        content,
        sendTime,
        status
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除聊天消息
  static async deleteChatMessage(messageID) {
    try {
      const response = await apiClient.delete(`/chatmessage/${messageID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
